package com.shopping.mall.core.module.language.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping.mall.core.module.language.entity.Language;
import com.shopping.mall.core.module.language.mapper.LanguageMapper;
import com.shopping.mall.core.module.language.service.LanguageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopping.mall.base.http.ResponseState;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.base.util.WrapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
/**
 * <p>
 *  Language服务实现类
 * </p>
 *
 * @author zxy
 * @since 2021-07-26
 */
@Validated
@Service
public class LanguageServiceImpl extends ServiceImpl<LanguageMapper, Language> implements LanguageService {

    @Autowired
    private LanguageMapper languageMapper;

    /**
     * 分页查询
     * @param current：页码
     * @param size：每页条数
     * @param language：筛选条件
     * @return IPage<Language>
     */
    @Override
    public IPage<Language> getLanguagePage(Integer current,Integer size,Language language){
        QueryWrapper<Language> queryWrapper = WrapperUtils.queryWrapper(language);
        Page<Language> languagePage = new Page<>(current,size);
        IPage<Language> languageIPage = languageMapper.selectPage(languagePage, queryWrapper);
        return languageIPage;
    }

    /**
     * 查询所有
     * @param language：筛选条件
     * @return <List<Language>
     */
    @Override
    public List<Language> getAll(Language language){
        QueryWrapper<Language> queryWrapper = WrapperUtils.queryWrapper(language);
        return languageMapper.selectList(queryWrapper);
    }

    /**
     * 通过id删除
     * @param id：languageId
     * @return Result<Object>
     */
    @Override
    public Result<Object> deleteById(String id){
        Language language=languageMapper.selectById(id);
        if(language==null){
            return Result.error(ResponseState.TARGET_NOT_EXIST.getValue(), ResponseState.TARGET_NOT_EXIST.getMessage());
        }
        languageMapper.deleteById(id);
        return Result.OK();
    }

    /**
    * 批量删除
    * @param ids：id列表
    * @return Result<Object>
    */
    @Override
    public Result<Object> batchDelete(List<String> ids){
        UpdateWrapper<Language> updateWrapper=new UpdateWrapper<>();
        updateWrapper.in("id",ids);
        languageMapper.delete(updateWrapper);
        return Result.OK();
    }
}